package com.turnengine.client.local.turn.command;

import com.javabi.command.IExecutableCommand;
import com.javabi.command.IExecutableCommandResponse;
import com.javabi.command.serializer.AbstractCommandDataSerializer;
import com.javabi.common.io.data.IDataReader;
import com.javabi.common.io.data.IDataWriter;
import com.turnengine.client.local.turn.enums.TurnsEnabled;
import java.io.IOException;

/**
 * The Set Turns Enabled Data Serializer.
 */
public class SetTurnsEnabledDataSerializer extends AbstractCommandDataSerializer<TurnsEnabled> implements ISetTurnsEnabledDataSerializer {

	/**
	 * Creates a new Set Turns Enabled Data Serializer.
	 */
	public SetTurnsEnabledDataSerializer() {
		super(SetTurnsEnabled.COMMAND_ID);
	}

	@Override
	public TurnsEnabled readReturnValue(IDataReader reader) throws IOException {
		TurnsEnabled value = reader.readEnum(TurnsEnabled.class, true);
		return value;
	}

	@Override
	public void writeReturnValue(IDataWriter writer, TurnsEnabled value) throws IOException {
		writer.writeEnum(value, true);
	}

	@Override
	public IExecutableCommand<TurnsEnabled> readRequest(IDataReader reader) throws IOException {

		// Read fields
		long loginId = reader.readLong();
		int instanceId = reader.readInt();
		TurnsEnabled updating = reader.readEnum(TurnsEnabled.class, true);

		// Create object
		SetTurnsEnabled object = new SetTurnsEnabled();
		object.setLoginId(loginId);
		object.setInstanceId(instanceId);
		object.setUpdating(updating);
		return object;
	}

	@Override
	public void writeRequest(IDataWriter writer, IExecutableCommand<TurnsEnabled> argument) throws IOException {
		SetTurnsEnabled object = (SetTurnsEnabled) argument;

		// Get fields
		long loginId = object.getLoginId();
		int instanceId = object.getInstanceId();
		TurnsEnabled updating = object.getUpdating();

		// Write fields
		writer.writeLong(loginId);
		writer.writeInt(instanceId);
		writer.writeEnum(updating, true);
	}

	@Override
	public IExecutableCommandResponse<TurnsEnabled> newResponse() {
		return new SetTurnsEnabledResponse();
	}
}
